<template>
  <div style="width: 500px">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item>
        <el-upload
          class="avatar-uploader"
          action="http://110.41.83.104/api/common/file/upload"
          :headers="headers"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.avatar" :src="form.avatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="邮箱">
        <el-input v-model="form.email" :disabled="true"></el-input>
      </el-form-item>

      <el-form-item label="昵称">
        <el-input v-model="form.userName"></el-input>
      </el-form-item>

      <div v-if="updatePass">
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>

        <el-form-item label="确认密码">
          <el-input v-model="form.password1"></el-input>
        </el-form-item>

        <el-form-item label="验证码">
          <el-input v-model="form.code">
            <el-button
              type="primary"
              style="color: white; background-color: #409eff"
              slot="append"
              >获取验证码</el-button
            >
          </el-input>
        </el-form-item>
      </div>

      <el-form-item>
        <el-button type="success" @click="onSubmit">立即更新</el-button>
        <el-button type="primary" @click="updatePass = true" v-if="!updatePass"
          >更改密码</el-button
        >
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from "@/api/apis/user.js";
export default {
  data() {
    return {
      updatePass: false,
      headers: { satoken: this.$store.state.token },
      action: "",
      form: {
        userName: "",
        email: "",
        avatar: "",
        password: "",
        password1: "",
        code: "",
      },
    };
  },
  mounted() {
    var baseURL = window.location.host;
    this.action = baseURL + "/api/common/file/upload";
    console.log(baseURL);

    this.userInfo();
  },
  methods: {
    userInfo() {
      const user = JSON.parse(this.$store.state.user);
      api.userInfo(user.id).then((res) => {
        this.form = res.data.data;
      });
    },
    reset() {
      this.updatePass = false;
      this.userInfo();
    },
    onSubmit() {
      api.updateInfo(this.form).then((res) => {
        var code = res.data.code;
        if (code == 200) {
          this.$message.success(res.data.data);
        } else {
          this.$message.error(res.data.data);
        }
      });
      console.log(this.form);
    },
    handleAvatarSuccess(res, file) {
      console.log(file);

      this.form.avatar = res.data;
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
